//
//  BCRefreshNormalHeader.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCRefreshNormalHeader.h"

static CGFloat BCRefreshNormalHeaderLayerWidth = 30.0f;


@interface BCRefreshNormalHeaderLayer : CAShapeLayer

/**
 *animateCircleLayer
 */
@property (nonatomic, strong) CAShapeLayer *animateCircleLayer;


- (void)setBackgroundCircleLayer;

@end

@implementation BCRefreshNormalHeaderLayer

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.animateCircleLayer = [CAShapeLayer layer];
        self.animateCircleLayer.fillColor = [UIColor clearColor].CGColor;
        self.animateCircleLayer.lineCap = kCALineCapRound;
        [self addSublayer:self.animateCircleLayer];
    }
    return self;
}

- (void)setLineWidth:(CGFloat)lineWidth {
    [super setLineWidth:lineWidth];
    self.animateCircleLayer.lineWidth = lineWidth;
}
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    self.animateCircleLayer.frame = self.bounds;
}


- (void)setBackgroundCircleLayer {
    self.path = [self circleBezierPathWithAngle:M_PI*1.5].CGPath;
}

- (UIBezierPath *)circleBezierPathWithAngle:(CGFloat)endAngle {
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    return [UIBezierPath bezierPathWithArcCenter:centerPoint radius:self.frame.size.width/2 startAngle:-M_PI/2 endAngle:endAngle clockwise:true];
}


- (void)updateAnimatelayerCirclePercent:(CGFloat)percent {
    percent = MAX(0, MIN(1, percent));
    //
    CGFloat angle = M_PI*2*percent-M_PI/2;
    self.animateCircleLayer.path = [self circleBezierPathWithAngle:angle].CGPath;
}


- (void)beginAnimate {
    [self endAnimate];
    //
    [self updateAnimatelayerCirclePercent:0.1];
    //
    CABasicAnimation *transform = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform.fromValue = @(0);
    transform.toValue = @(M_PI*4);
    transform.duration = 2;
    transform.repeatCount = HUGE_VALF;
    transform.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transform.removedOnCompletion = false;
    [self.animateCircleLayer addAnimation:transform forKey:@""];
    
    //
    CABasicAnimation *transform2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    transform2.fromValue = @(0);
    transform2.toValue = @(M_PI*2);
    transform2.duration = transform.duration*5;
    transform2.repeatCount = HUGE_VALF;
    transform2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transform2.removedOnCompletion = false;
    [self addAnimation:transform2 forKey:@""];
    
}

- (void)endAnimate {
    [self removeAllAnimations];
    [self.animateCircleLayer removeAllAnimations];
    //
    [self updateAnimatelayerCirclePercent:0];
}

@end


@interface BCRefreshNormalHeader ()

@property (nonatomic, strong) BCRefreshNormalHeaderLayer *refreshLayer;

@end

@implementation BCRefreshNormalHeader
/** 初始化 */
- (void)prepare {
    [super prepare];
    //
    self.refreshLayer = [BCRefreshNormalHeaderLayer layer];
    self.refreshLayer.fillColor = [UIColor clearColor].CGColor;
    self.refreshLayer.lineWidth = 3;
    self.refreshLayer.strokeColor = [UIColor separatorColor].CGColor;
    [self.layer addSublayer:self.refreshLayer];
}
/** 摆放子控件frame */
- (void)placeSubviews {
    [super placeSubviews];
    //
    CGFloat width = BCRefreshNormalHeaderLayerWidth;
    //
    self.refreshLayer.frame = CGRectMake(self.frame.size.width/2-width/2, self.frame.size.height/2-width/2, width, width);
    //设置背景圈圈
    [self.refreshLayer setBackgroundCircleLayer];
    //
    self.refreshLayer.animateCircleLayer.strokeColor = self.animateCircleClolor.CGColor?:[UIColor darkMainColor].CGColor;
}

- (void)setAnimateCircleClolor:(UIColor *)animateCircleClolor {
    _animateCircleClolor = animateCircleClolor;
    //
    self.refreshLayer.animateCircleLayer.strokeColor = animateCircleClolor.CGColor;
}


/** 当scrollView的contentOffset发生改变的时候调用 */
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
    
    if (!self.isRefreshing) {
        [self.refreshLayer updateAnimatelayerCirclePercent:self.pullingPercent];
    }
}
#pragma mark -
- (void)beginRefreshing {
    [super beginRefreshing];
    //
    [self.refreshLayer beginAnimate];
}
- (void)endRefreshing {
    [super endRefreshing];
    //
    [self.refreshLayer endAnimate];
}

@end
